/*************************/
/** 仅支持渲染进程引入使用 **/
/************************/

import type { RendererEnv } from '../../main/env';
import { IpcMainEventEnum, IpcRendererEventEnum } from '../enum';

const { ipcRenderer, clipboard } = window.electron;

function fixPathSeparator(path: string): string {
    return path.replace(/\/|\\/g, '/');
}

/**
 * 获取环境变量
 */
function getRendererEnv(): Promise<RendererEnv> {
    return new Promise(resolve => {
        ipcRenderer.once(IpcRendererEventEnum.RENDERER_ENV, (_, result) => {
            resolve(result);
        });
        ipcRenderer.send(IpcMainEventEnum.RENDERER_ENV_READY);
    });
}

/**
 * 打开选择对话框
 */
function showOpenDialog(options: { openPath?: string; type: 'selectFile' | 'selectDir' }): Promise<{
    paths: string[];
    isCancel: boolean;
}> {
    // ...
    return new Promise((resolve, reject) => {
        ipcRenderer.once(IpcRendererEventEnum.OPEN_DIALOG_RESULT, (_, result) => {
            if (result.isCancel || !(result.paths || []).length) {
                reject();
            } else {
                result.paths = result.paths.map((p: string) => {
                    return fixPathSeparator(p);
                });
                resolve(result);
            }
        });
        ipcRenderer.send(IpcMainEventEnum.OPEN_DIALOG, options || {});
    });
}

/**
 * 打开资源所在目录
 */
function showItemInFolder(path: string): void {
    path = path.replace(/\/|\\/g, '/');
    ipcRenderer.send(IpcMainEventEnum.OPEN_FOLDER, { path });
}

/**
 * 执行命令
 */
function runCommands(params: { cwd?: string; async: boolean; commands: string[] }): Promise<
    Array<{
        command: string;
        stdout?: string;
        stderr?: string;
    }>
> {
    // ...
    return new Promise((resolve, reject) => {
        ipcRenderer.once(IpcRendererEventEnum.RUN_COMMANDS_FINISH, (_, result) => {
            resolve(result);
        });
        ipcRenderer.once(IpcRendererEventEnum.RUN_COMMANDS_ERROR, (_, error) => {
            reject(error);
        });
        ipcRenderer.send(IpcMainEventEnum.RUN_COMMANDS, params);
    });
}

/**
 * 执行脚本
 */
function execScript(params: { cwd?: string; scriptContent: string }): Promise<{ stdout?: string; stderr?: string }> {
    // ...
    return new Promise((resolve, reject) => {
        ipcRenderer.once(IpcRendererEventEnum.EXEC_SCRIPT_FINISH, (_, result) => {
            resolve(result);
        });
        ipcRenderer.once(IpcRendererEventEnum.EXEC_SCRIPT_ERROR, (_, error) => {
            reject(error);
        });
        ipcRenderer.send(IpcMainEventEnum.EXEC_SCRIPT, params);
    });
}

/**
 * 读取文件内容
 */
function readFile(filepath: string): Promise<{ content: string }> {
    return new Promise(resolve => {
        ipcRenderer.once(IpcRendererEventEnum.READ_FILE_RESULT, (_, result) => {
            resolve(result);
        });
        ipcRenderer.send(IpcMainEventEnum.READ_FILE, { filepath });
    });
}

/**
 * 写入剪贴板内容
 */
function clipboardWriteText(content: string) {
    clipboard.writeText(content);
}

// as electronUtils
export default {
    ipcRenderer,

    getRendererEnv,

    readFile,
    clipboardWriteText,

    execScript,
    runCommands,

    showOpenDialog,
    showItemInFolder,
};
